package com.tancire.sources.controller;

import com.tancire.common.annotation.Client;
import com.tancire.common.annotation.Log;
import com.tancire.common.core.controller.BaseController;
import com.tancire.common.core.domain.AjaxResult;
import com.tancire.common.core.page.TableDataInfo;
import com.tancire.common.enums.BusinessType;
import com.tancire.common.enums.UserSource;
import com.tancire.common.exception.ResourceException;
import com.tancire.common.utils.poi.ExcelUtil;
import com.tancire.file.bean.SaveFileBean;
import com.tancire.sources.domain.SourceCode;
import com.tancire.sources.domain.SurFile;
import com.tancire.sources.service.ISurFileService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 文件管理Controller
 *
 * @author chenchao
 * @date 2024-08-18
 */
@RestController
@RequestMapping("/sources/member/file")
public class SurMemberFileController extends BaseController {
    @Resource
    private ISurFileService surFileService;

    /**
     * 查询文件管理列表
     */
    @Client(UserSource.MEMBER)
    @PreAuthorize("@ss.hasPermi('member:user:union')")
    @GetMapping("/list")
    public TableDataInfo list(SurFile surFile) {
        startPage();
        List<SurFile> list = surFileService.selectSurFileList(surFile);
        return getDataTable(list);
    }

    /**
     * 查询文件管理列表
     */
    @Client(UserSource.MEMBER)
    @PreAuthorize("@ss.hasPermi('member:user:union')")
    @GetMapping("/listAll")
    public AjaxResult listAll(SurFile surFile) {
        List<SurFile> list = surFileService.selectSurFileList(surFile);
        return AjaxResult.success("查询成功", list);
    }

    /**
     * 查询当前目录下文件夹及文件
     */
    @Client(UserSource.MEMBER)
    @PreAuthorize("@ss.hasPermi('member:user:union')")
    @GetMapping("/child/node/{pid}")
    public AjaxResult getChildNode(@PathVariable("pid") Long pid) {
        List<SurFile> list = surFileService.getChildNode(pid);
        return AjaxResult.success("查询成功", list);
    }

    /**
     * 移动文件夹位置
     */
    @Client(UserSource.MEMBER)
    @PreAuthorize("@ss.hasPermi('member:user:union')")
    @GetMapping("/move/nodes")
    public AjaxResult moveDirOrFile(@RequestBody List<SurFile> surFileList) {
        surFileService.moveDirOrFile(surFileList);
        return AjaxResult.success("移动成功");
    }

    /**
     * 复制文件夹
     */
    @Client(UserSource.MEMBER)
    @PreAuthorize("@ss.hasPermi('member:user:union')")
    @GetMapping("/copy/nodes")
    public AjaxResult copyDirOrFile(@RequestBody List<SurFile> surFileList) {
        surFileService.copyDirOrFile(surFileList);
        return AjaxResult.success("复制成功");
    }

    /**
     * 导出文件管理列表
     */
    @Client(UserSource.MEMBER)
    @PreAuthorize("@ss.hasPermi('member:user:union')")
    @Log(title = "文件管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SurFile surFile) {
        List<SurFile> list = surFileService.selectSurFileList(surFile);
        ExcelUtil<SurFile> util = new ExcelUtil<SurFile>(SurFile.class);
        util.exportExcel(response, list, "文件管理数据");
    }

    /**
     * 获取文件管理详细信息
     */
    @Client(UserSource.MEMBER)
    @PreAuthorize("@ss.hasPermi('member:user:union')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(surFileService.selectSurFileById(id));
    }

    /**
     * 新增文件管理
     */
    @Client(UserSource.MEMBER)
    @PreAuthorize("@ss.hasPermi('member:user:union')")
    @Log(title = "文件管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SurFile surFile) {
        return toAjax(surFileService.insertSurFile(surFile));
    }

    /**
     * 修改文件管理
     */
    @Client(UserSource.MEMBER)
    @PreAuthorize("@ss.hasPermi('member:user:union')")
    @Log(title = "文件管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SurFile surFile) {
        return toAjax(surFileService.updateSurFile(surFile));
    }

    /**
     * 删除文件管理
     */
    @Client(UserSource.MEMBER)
    @PreAuthorize("@ss.hasPermi('member:user:union')")
    @Log(title = "文件管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(surFileService.deleteSurFileByIds(ids));
    }

    /**
     * 获取预览下载授权码
     */
    @Client(UserSource.MEMBER)
    @PreAuthorize("@ss.hasPermi('member:user:union')")
    @GetMapping("/auth/code/{id}")
    public AjaxResult getViewCode(@PathVariable("id") Long id) {
        SourceCode code = new SourceCode();
        try {
            code = surFileService.getViewCode(id);
        } catch (Exception e) {
            code.setAuthStatus(3);
            code.setMsg("获取授权码错误");
            return AjaxResult.success(code);
        }
        return AjaxResult.success(code);
    }

    /**
     * 上传
     *
     * @param file
     * @return
     */
    @Client(UserSource.MEMBER)
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("file") MultipartFile file, @RequestParam("pid") Long pid) {
        try {
            SaveFileBean saveFileBean = surFileService.uploadFile(file, pid, UserSource.MEMBER.getCode());
            return AjaxResult.success(saveFileBean);
        } catch (Exception e) {
            return AjaxResult.error("上传文件异常:" + e.getMessage());
        }
    }

    /**
     * 视频预览接口
     *
     * @param code
     * @param req
     * @param res
     * @return
     */
    @Client(UserSource.MEMBER)
    @RequestMapping("/video/view/{code}")
    public void view(@PathVariable("code") String code, HttpServletRequest req, HttpServletResponse res) {
        // 从redis取资源
        try {
            this.surFileService.view(code, req, res);
        } catch (Exception e) {
            throw new ResourceException(e.getMessage());
        }
    }

    /**
     * 下载接口
     *
     * @param code
     * @param req
     * @param res
     * @return
     */
    @RequestMapping("/common/download/{code}")
    public void download(@PathVariable("code") String code, HttpServletRequest req, HttpServletResponse res) {
        // 从redis取资源
        try {
            this.surFileService.download(code, req, res);
        } catch (Exception e) {
            throw new ResourceException(e.getMessage());
        }
    }


}
